<template>
	<view class="box">
		<!-- 导航栏 -->
		<u-navbar title-color="#fff" back-icon-color="#ffffff" is-back="true" :background="background" :back-text-style="{ color: '#fff' }"
		 title="内存空间管理" back-icon-name="nav-back">
			<view class="navbar-right" slot="right">
				<view class="dot-box right-item fo-white paddlr20">
					<!-- <text class="cuIcon-refresh fo-46 mr20" @click="AsyncData"></text> -->
					<!-- <text class="cuIcon-roundadd fo-46" @click="onAdd"></text> -->
				</view>
			</view>
		</u-navbar>
		<view class="mt30">
			<view class="font-wei600 fo-40">洋盘已使用空间</view>
			<view class="font-wei600 fo-50 mt20">{{$tool.comFileSize(AllSize)}}</view>
			<text class="fo-gray mt20">占用手机存储空间不足1%的存储空间</text>
		</view>
		<view class="flex flex-wrap mt30">
			<view class="flex ali-sta mr100">
				<view style="height: 30rpx; width: 30rpx;" class="bg-Crimson mr10"></view>
				<view class="flex flex-col">
					<text>缓存</text>
					<text class="fo-gray">{{$tool.comFileSize(Cache)}}</text>
				</view>
			</view>
			<view class="flex ali-sta mr100">
				<view style="height: 30rpx; width: 30rpx;" class="bg-Yellow mr10"></view>
				<view class="flex flex-col">
					<text>聊天数据</text>
					<text class="fo-gray">待完善</text>
				</view>
			</view>
			<view class="flex ali-sta">
				<view style="height: 30rpx; width: 30rpx;" class="bg-DimGray mr10"></view>
				<view class="flex flex-col">
					<text>运行所需文件</text>
					<text class="fo-gray">待完善</text>
				</view>
			</view>
		</view>
		<u-button class="mr20 ml20 mt40" type="primary" @click="claerCache">清理洋盘缓存</u-button>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				background: {
					background: '#87ceeb'
				},
				root: '', // 当前的绝对路径
				AllSize: "" ,// 本app的总占用空间
				Cache:0 ,// 缓存空间
				
				downloadCache:"" // 公用下载文件夹的路径
			}
		},
		onLoad() {
			this.init()
		},
		methods: {
			async init(){
				// #ifdef APP-PLUS
				// 获取运行应用占用的空间
				plus.io.requestFileSystem(plus.io.PUBLIC_DOCUMENTS, async (fs) => {
						this.root = fs.root.fullPath.split('/').slice(0, -2).join('/')
						let FileIno = await this.GetFileInfo_for_URL(this.root)
						this.AllSize = FileIno.size
						this.downloadCache = this.root + '/' + 'downloads'
						
						// 自定义下载文件路径 和apk。wgt包路径
						// let YangCache = this.downloadCache + '/yang'
						// let ApkCache = this.downloadCache + '/apk'
						FileIno = await this.GetFileInfo_for_URL(this.downloadCache)
						this.Cache += FileIno.status? FileIno.size : 0
						// FileIno = await this.GetFileInfo_for_URL(ApkCache)
						// this.Cache += FileIno.size
						
					},
					function(e) {
						plus.nativeUI.alert("Request file system failed:" + e.message);
					});
				// 获取运行缓存的空间
				plus.cache.calculate((size) => { //size是多少个字节单位是b
					this.Cache = size
				});
				
				
				
				// #endif
			},
			// 传入绝对路径 返回该文件/目录信息
			GetFileInfo_for_URL(URL) {
				/*
					status:是否获取成功,
					name:文件名字/文件夹名字
					size:文件大小
					file:文件的数量
					dir:子目录的数量
					modifiTime:操作时间
					fullPath:绝对路径
					isFile:是否是文件
					entry:目录对象
				*/
				return new Promise((resolve, reject) => {
					plus.io.resolveLocalFileSystemURL(URL, (entry) => {
						entry.getMetadata((metadata) => {
							resolve({
								status: true,
								name: entry.name || (/\/$/g).test(URL) ? URL.split('/').slice(-2)[0] : URL.split('/').slice(-1)[0],
								size: metadata.size,
								fileCount: metadata.fileCount,
								directoryCount: metadata.directoryCount,
								modifiTime: metadata.modificationTime,
								fullPath: URL,
								isFile: entry.isFile,
								entry
							})
						}, (e) => {
							// plus.nativeUI.alert("该目录/文件不存在~：" + e.message);
							resolve({
								status: false
							})
						}, true);
					}, (e) => {
						// plus.nativeUI.alert("该目录/文件不存在~：" + e.message);
						resolve({
							status: false
						})
					})
				})
			},
			// 清空缓存
			claerCache(){
				let self = this
				//可以询问用户是否删除
				uni.showModal({
					title: '提示',
					content: '确定清除缓存吗?',
					success(res) {
						// 用户确定要删除
						if (res.confirm) {
							//使用plus.cache.clear 清除应用中的缓存数据 这里清除后还要二十几KB没有清除，达不到全部清除
							plus.cache.clear(async()=>{
								let delFN = (URL)=>{
									let delDirName = 'downloads'
									return new Promise(async(res,rej)=>{
										let FileInfo = await self.GetFileInfo_for_URL(URL)
										// 递归删除
										FileInfo.entry.removeRecursively((entry)=>{
											res({
												status:true,msg:"文件删除成功"
											})
										},(e)=>{
											res({
												status:false,msg:"文件删除失败"+e.message
											})
										})
										if(delDirName==='documents' || delDirName==='downloads'){
											let predir =  (/\/$/g).test(URL)?  URL.split('/').slice(0,-2).join('/'):URL.split('/').slice(0,-1).join('/')
											// 创建文件夹
											FileInfo = await self.GetFileInfo_for_URL(predir)
											FileInfo.entry.getDirectory( delDirName,  {create:true,exclusive:false}, function( dir ){
												console.log("Directory Entry Name: " + dir.name);
											}, function (e) {
												console.log( e.message );
											} );
										}
									})
								}
								await delFN(self.downloadCache)
								uni.showToast({
									title: '清除成功',
									icon: 'none',
									success() {
										self.init()
									}
								})
							});
						}
					}
				})
			}
		}
	}
</script>

<style scoped>
	.box{
		padding: 20rpx;
	}
</style>
